/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FramePrincipal.java
 *
 * Created on 10/08/2011, 20:30:29
 */
package br.unicamp.ft.robustez;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author marcelo
 */
public class FramePrincipal extends javax.swing.JFrame {

    private Communicator cm;
    
    /** Creates new form FramePrincipal */
    public FramePrincipal() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlPrincipal = new javax.swing.JScrollPane();
        txtLog = new javax.swing.JTextArea();
        pnlInferior = new javax.swing.JPanel();
        txtMensagem = new javax.swing.JTextField();
        btnEnviar = new javax.swing.JButton();
        mnuBar = new javax.swing.JMenuBar();
        mnuArquivo = new javax.swing.JMenu();
        mnuIniciar = new javax.swing.JMenuItem();
        mnuConectar = new javax.swing.JMenuItem();
        mnuDesconectar = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Neem : Aplicação de Testes");

        txtLog.setColumns(20);
        txtLog.setRows(5);
        pnlPrincipal.setViewportView(txtLog);

        getContentPane().add(pnlPrincipal, java.awt.BorderLayout.CENTER);

        pnlInferior.setLayout(new java.awt.BorderLayout());
        pnlInferior.add(txtMensagem, java.awt.BorderLayout.CENTER);

        btnEnviar.setText("Enviar Mensagem");
        btnEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEnviarActionPerformed(evt);
            }
        });
        pnlInferior.add(btnEnviar, java.awt.BorderLayout.LINE_END);

        getContentPane().add(pnlInferior, java.awt.BorderLayout.PAGE_END);

        mnuArquivo.setText("Arquivo");

        mnuIniciar.setText("Iniciar Rede");
        mnuIniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuIniciarActionPerformed(evt);
            }
        });
        mnuArquivo.add(mnuIniciar);

        mnuConectar.setText("Conectar...");
        mnuConectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuConectarActionPerformed(evt);
            }
        });
        mnuArquivo.add(mnuConectar);

        mnuDesconectar.setText("Desconectar");
        mnuDesconectar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuDesconectarActionPerformed(evt);
            }
        });
        mnuArquivo.add(mnuDesconectar);

        mnuBar.add(mnuArquivo);

        setJMenuBar(mnuBar);

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void mnuConectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuConectarActionPerformed
// TODO add your handling code here:
    String remoteNode = javax.swing.JOptionPane.showInputDialog("Digite o nome ou IP remoto de um nó"); 
    this.cm = new Communicator(this,remoteNode);
}//GEN-LAST:event_mnuConectarActionPerformed

private void mnuIniciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuIniciarActionPerformed
    this.cm = new Communicator(this);
}//GEN-LAST:event_mnuIniciarActionPerformed

private void mnuDesconectarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuDesconectarActionPerformed
    this.cm.close();
    mostraLog("Stopped");
}//GEN-LAST:event_mnuDesconectarActionPerformed

private void btnEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEnviarActionPerformed
        try {
            cm.sendMessage(txtMensagem.getText());
        } catch (IOException ex) {
            Logger.getLogger(FramePrincipal.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_btnEnviarActionPerformed


public void mostraLog(String message){
    txtLog.append(message + "\n");
}
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FramePrincipal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FramePrincipal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEnviar;
    private javax.swing.JMenu mnuArquivo;
    private javax.swing.JMenuBar mnuBar;
    private javax.swing.JMenuItem mnuConectar;
    private javax.swing.JMenuItem mnuDesconectar;
    private javax.swing.JMenuItem mnuIniciar;
    private javax.swing.JPanel pnlInferior;
    private javax.swing.JScrollPane pnlPrincipal;
    private javax.swing.JTextArea txtLog;
    private javax.swing.JTextField txtMensagem;
    // End of variables declaration//GEN-END:variables
}
